package system

import (
	"context"
	"go-admin/app/global/app"
	"go-admin/app/models"
	"gorm.io/gorm"
)

type SysUsers struct {
	models.BaseModel
	Username    string      `gorm:"column:username;uniqueIndex;not null;size:50;comment:用户名" json:"userName"`
	Password    string      `gorm:"column:password;not null;size:255;comment:密码" json:"passWord"`
	Email       string      `gorm:"column:email;size:100;comment:邮箱" json:"email"`
	Status      int8        `gorm:"column:status;default:1;comment:是否启用 0停用 1启用" json:"status"`
	Description string      `gorm:"column:description;not null;size:500;comment:描述" json:"description"`
	DeptID      uint        `gorm:"column:dept_id;default:0;comment:部门ID" json:"deptId"`
	Phone       string      `gorm:"column:phone;size:64;comment:电话" json:"phone"`
	Sex         string      `gorm:"column:sex;size:64;comment:性别" json:"sex"`
	NickName    string      `gorm:"column:nick_name;size:100;comment:昵称" json:"nickName"`
	Avatar      string      `gorm:"column:avatar;size:255;comment:头像" json:"avatar"`
	CreatedBy   uint        `gorm:"column:created_by;default:0;comment:创建人" json:"createdBy"`
	Roles       SysRoleList `gorm:"many2many:sys_user_role;foreignKey:id;joinForeignKey:user_id;references:id;joinReferences:role_id" json:"roles"`
	Dept        SysDept     `gorm:"foreignKey:dept_id;references:id" json:"dept"`
	TenantID    uint        `gorm:"type:int(11);column:tenant_id;comment:租户ID" json:"tenantID"`
	Tenant      SysTenants  `gorm:"foreignKey:tenant_id;references:id" json:"tenant"`
}
type SysUserList []*SysUsers

// TableName 设置User表名
func (SysUsers) TableName() string {
	return "sys_users"
}

func NewUser() *SysUsers {
	return &SysUsers{}
}

func (u *SysUsers) IsEmpty() bool {
	return u == nil || u.ID == 0
}

func (u *SysUsers) Find(ctx context.Context, funcs ...func(*gorm.DB) *gorm.DB) error {
	return app.DB().WithContext(ctx).Scopes(funcs...).Find(u).Error
}

func (u *SysUsers) GetUserByID(ctx context.Context, id uint) (err error) {
	return u.Find(ctx, func(db *gorm.DB) *gorm.DB {
		return db.Where("id = ?", id)
	})
}

func (u *SysUsers) GetUserByUsername(ctx context.Context, username string) (err error) {
	return u.Find(ctx, func(db *gorm.DB) *gorm.DB {
		return db.Where("username = ?", username)
	})
}

// GetUserByPhone 根据手机号获取用户
func (u *SysUsers) GetUserByPhone(ctx context.Context, phone string) (err error) {
	return u.Find(ctx, func(db *gorm.DB) *gorm.DB {
		return db.Where("phone = ?", phone)
	})
}

// GetUserByEmail 根据邮箱获取用户
func (u *SysUsers) GetUserByEmail(ctx context.Context, email string) (err error) {
	return u.Find(ctx, func(db *gorm.DB) *gorm.DB {
		return db.Where("email = ?", email)
	})
}

func (u *SysUsers) Create(ctx context.Context, funcs ...func(*gorm.DB) *gorm.DB) (err error) {
	return app.DB().WithContext(ctx).Scopes(funcs...).Create(u).Error
}

// DeleteByID 根据ID软删除用户
func (u *SysUsers) DeleteByID(ctx context.Context, id uint) error {
	return app.DB().WithContext(ctx).Where("id = ?", id).Delete(u).Error
}

func NewUserList() SysUserList {
	return SysUserList{}
}

func (list *SysUserList) Find(ctx context.Context, funcs ...func(*gorm.DB) *gorm.DB) (err error) {
	err = app.DB().WithContext(ctx).Scopes(funcs...).Find(list).Error
	return

}

func (list *SysUserList) GetTotal(ctx context.Context, query ...func(*gorm.DB) *gorm.DB) (int64, error) {
	var total int64
	err := app.DB().WithContext(ctx).Model(&SysUsers{}).Scopes(query...).Count(&total).Error
	if err != nil {
		return 0, err
	}
	return total, nil
}

func UserCount(ctx context.Context, query ...func(*gorm.DB) *gorm.DB) (int64, error) {
	var total int64
	err := app.DB().WithContext(ctx).Model(&SysUsers{}).Scopes(query...).Count(&total).Error
	if err != nil {
		return 0, err
	}
	return total, nil
}
